<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
            background-color: #f8f8f8;
        }
        .content .content_nav {
            width: 90px;
            height: 100%;
            background-color: #f8f8f8;
            position: fixed;
            left: 0;
            top: 0;
            bottom: 0;
            overflow-y: scroll;
            -webkit-overflow-scrolling: touch;
        }
        .content .content_nav::-webkit-scrollbar{
          display: none;
        }
        .content .content_nav ul li {
            /* height: 45px; */
            /* line-height: 45px; */
            border-bottom: 1px solid #fff;
            font-size: 14px;
            padding: 12px 12px;
            color: #333;
             overflow: hidden; 
            /* text-overflow: ellipsis; */
            /* white-space: nowrap; */
            text-align: center;
        }
        .content .content_nav ul li.active {
            background-color: #fff;
        }
        .content .content_main {
            height: 100%;
            background-color: #fff;
            padding-left: 10px;
            position: fixed;
            left: 90px;
            right: 0;
            top: 0;
            bottom: 0;
            overflow-y: scroll;
            -webkit-overflow-scrolling: touch;
        }
        .content .content_main::-webkit-scrollbar{
          display: none;
        }
        .content .content_main ul li .li_box {
            height: 75px;
            border-bottom: 1px solid #e8e8e8;
            overflow: hidden;
            display: -webkit-box;
            display: -webkit-flex;
        }
        .content .content_main ul li .li_box .li_boxl {
            width: 0;
            height: 75px;
            padding-top: 10px;
            padding-bottom: 10px;
            box-sizing: border-box;
            flex: 1;
            -webkit-flex: 1;
            box-flex: 1;
            -webkit-box-flex: 1;
            display: -webkit-box;
            display: -webkit-flex;
            align-items: center;
        }
        .content .content_main ul li .li_box .li_boxl .li_img .img_box {
            width: 55px;
            height: 55px;
            border-radius: 5px;
            overflow: hidden;
        }
        .content .content_main ul li .li_box .li_boxl .li_img .img_box img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        .content .content_main ul li .li_box .li_boxl .li_info {
            width: 0;
            padding-left: 10px;
            flex: 1;
            -webkit-flex: 1;
            box-flex: 1;
            -webkit-box-flex: 1;
            padding-right: 5px;
        }
        .content .content_main ul li .li_box .li_boxl .li_info .title {
            font-size: 15px;
            color: #333;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .content .content_main ul li .li_box .li_boxl .li_info .desc {
            font-size: 12px;
            color: #999;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .content .content_main ul li .li_box .li_boxl .li_info .price {
            color: #e62442;
            font-size: 16px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .content .content_main ul li .li_box .li_boxr {
            height: 75px;
            padding-right: 4px;
            display: -webkit-box;
            -webkit-box-align: center;
        }
        .content .content_main ul li .li_box .li_boxr .li_btn {
            height: 75px;
            overflow: hidden;
        }
        .content .content_main ul li .li_box .li_boxr .li_btn img {
            height: 22px;
            display: block;
        }
        .content .content_main ul li .li_box .li_boxr .li_btn .min {
            height: 75px;
            float: left;
            padding: 0 11px;
            display: -webkit-box;
            -webkit-box-align: center;
            display: none;
        }
        .content .content_main ul li .li_box .li_boxr .li_btn .num {
            height: 75px;
            line-height: 75px;
            float: left;
            color: #333;
            font-size: 13px;
            display: -webkit-box;
            -webkit-box-align: center;
            display: none;
        }
        .content .content_main ul li .li_box .li_boxr .li_btn .add {
            height: 75px;
            float: left;
            padding: 0 11px;
            display: -webkit-box;
            -webkit-box-align: center;
        }
        .hidden_img {
            display: none;
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="content_nav">
            <ul id="menu_ul">
                <li class="active" tapmode onclick="fnClick(0);"></li>
            </ul>
        </div>
        <div class="content_main" id="goods_box">
<!--             <ul id="goods_ul">
                <li>
                    <div class="li_box">
                        <div class="li_boxl" tapmode onclick="fnFoodDetail();">
                            <div class="li_img">
                                <div class="img_box">
                                    <img src="../image/temp/1.png">
                                </div>
                            </div>
                            <div class="li_info">
                                <div class="title">菜品名称</div>
                                <div class="desc">菜品介绍</div>
                                <div class="price" data="20">RM20</div>
                            </div>
                        </div>
                        <div class="li_boxr">
                            <div class="li_btn">
                                <div class="min" tapmode onclick="fnMin(this);">
                                    <img src="../image/min.png">
                                </div>
                                <div class="num" data="0">0</div>
                                <div class="add" tapmode onclick="fnAdd(this);">
                                    <img src="../image/add.png">
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
            </ul> -->
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script id="catsTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        <li id="cat_{{=it[i]['id']}}" cat_id="{{=it[i]['id']}}" {{if(i==0){ }} class="active"{{ }; }} tapmode onclick="fnClick({{=i}},{{=it[i]['id']}});">{{=it[i]['category_name']}}</li>

    {{ }; }}
</script>
<script id="goodsTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        <ul id="goods_ul_{{=it[i]['cat_id']}}" style="display: {{if(i==0){ }}block; {{ }else{  }} none; {{ };  }}">
            {{for(var j=0;j<it[i]['goods'].length;j++){ }}
                <li>
                    <div class="li_box">
                        <div class="li_boxl" tapmode onclick="fnFoodDetail({{=it[i]['goods'][j]['id']}});">
                            <div class="li_img">
                                <div class="img_box">
                                    <img src="../image/default.png">
                                    <img onload="setImg(this);" class="hidden_img" src="{{=formatVideoImg(it[i]['goods'][j]['goods_img'])}}">
                                </div>
                            </div>
                            <div class="li_info">
                                <div class="title">{{=it[i]['goods'][j]['goods_name']}}</div>
                                <div class="desc">{{=it[i]['goods'][j]['desc']}}</div>
                                <div class="price" data="20">RM{{=it[i]['goods'][j]['price']}}</div>
                            </div>
                        </div>
                        <div class="li_boxr">
                            <div class="li_btn">
                                <div class="min" tapmode onclick="fnMin(this,{{=it[i]['goods'][j]['id']}},{{=it[i]['goods'][j]['price']}});">
                                    <img src="../image/min.png">
                                </div>
                                <div class="num" data="0">0</div>
                                <div id="goods{{=it[i]['goods'][j]['id']}}" class="add" tapmode onclick="fnAdd(this,{{=it[i]['goods'][j]['id']}},{{=it[i]['goods'][j]['price']}});">
                                    <img src="../image/add.png">
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
            {{ }; }}
        </ul>
    {{ }; }}
</script>
<script type="text/javascript">
    apiready = function(){
        id = api.pageParam.id;
        pre_goods_id  = api.pageParam.goods_id;
        pre_price = api.pageParam.price;
        
        now_cat = '';
        openLoading();
        fnGetshopGoods();

        //详情页加入购物车
        api.addEventListener({
            name: 'addCartEvent'
        }, function(ret, err) {
            if (ret) {
                var chose_id = ret.value.goods_id;
                var price = parseFloat(ret.value.price);
                fnAdd('#goods'+chose_id,chose_id,price);
            }
        });
        if(pre_goods_id && pre_price){
            // 是在优惠活动的商品列表里点击进来的
            setTimeout(function(){
                closeLoading();
                fnAdd('#goods'+pre_goods_id,pre_goods_id,pre_price);
            },800)
        }
    };

    // 菜品详情
    function fnFoodDetail(id) {
        api.openWin({
            name: 'food_detail',
            url: 'food_detail.html',
            pageParam : {
                id: id
            }
        });
    }

    function fnClick(_index,cat_id) {
        fnChange(_index);
        if(now_cat != cat_id) {
            $('#goods_box ul').hide();
            $('#goods_ul_'+cat_id).show();
            now_cat = cat_id;
        }
    }

    function fnChange(_index) {
        if (!isNaN(_index)) {
            this_ = $('.content_nav ul li').eq(_index);
        }
        $('.content_nav ul li').removeClass('active');
        $(this_).addClass('active');
        var scroll = $(this_).offset().top + $('.content_nav').scrollTop() - $('.content_nav').height() / 2 + $(this_).height() / 2;
        $('.content_nav').scrollTop(scroll);
    }
    var goodsObj = {};
    var allPrice = 0;
    var mark = true;
    function fnAdd(this_,goods_id,price) {
        if(mark == false) {
            return;
        }
        openLoading();
        mark = false;
        var num = $(this_).siblings('.num').attr('data');
        // 先检查一下是否有足够的库存 - tim
        api.ajax({
            url: rootPath+'/api/goods/check_stock',
            method: 'post',
            data: {
                values: { 
                    goods_id: goods_id,
                    id: id,
                    num: num
                },
            }
        }, function(ret, err) {
            if (ret && ret.status == 200) {
                ++num;
                $(this_).siblings('.num').attr('data', num);
                $(this_).siblings('.num').text(num);
                if (num > 0) {
                    $(this_).siblings('.min').css('display', '-webkit-box');
                    $(this_).siblings('.num').css('display', 'block');
                }
                //插到对象中，用于生产订单
                goodsObj[goods_id] = parseInt(num);
                allPrice = parseFloat(allPrice) + parseFloat(price);
                allPrice = allPrice.toFixed(2);
                
                api.sendEvent({
                    name: 'allPrice',
                    extra: {
                        allPrice: allPrice,
                        goodsObj : goodsObj
                    }
                });
            } else {
                api.toast({
                    msg: ret.msg,
                    location: 'middle'
                });
            }
            mark = true;
            closeLoading();
        });
        
    }

    function fnMin(this_,goods_id,price) {
        var num = $(this_).siblings('.num').attr('data');
        --num;
        $(this_).siblings('.num').attr('data', num);
        $(this_).siblings('.num').text(num);
        if (num <= 0) {
            $(this_).css('display', 'none');
            $(this_).siblings('.num').css('display', 'none');
        }
        if(num == 0) {
            delete goodsObj[goods_id];
        }else {
            goodsObj[goods_id] = parseInt(num);
        }
        allPrice = parseFloat(allPrice) - parseFloat(price);
        allPrice = allPrice.toFixed(2);
        
        api.sendEvent({
            name: 'allPrice',
            extra: {
                allPrice: allPrice,
                goodsObj:goodsObj,
            }
        });
    }

    function fnGetshopGoods() {
        api.ajax({
            url: rootPath+'/api/shop/get_shop_goods',
            method: 'get',
            data: {
                values: { 
                    shop_id: id
                },
            }
        }, function(ret, err) {
            if (ret) {
                if(ret.cats) {
                    if(ret['cats'].length > 0) {
                        now_cat = ret['cats'][0]['id'];
                    }
                    if(ret['cats']) {
                        setDotData(ret.cats,'catsTpl','menu_ul',true);
                    }
                    if(ret['goods']) {
                        setDotData(ret.goods,'goodsTpl','goods_box',true);
                        fnChange(0);
                    }
                }
            } else {
                showError(err.code,err.msg);
            }
            closeLoading();
        });
    }

    function fnGetSubGoods(cat_id) {

    }

    function isFloat(obj) { 
        obj = obj+'';
        if(obj.indexOf('.') > 0) {
            return true;
        }else {
            return false;
        }
    } 
</script>
</html>